package dao;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import bean.BlogBean;
import bean.CommentBean;
import bean.LastIdsBean;
import bean.MediaBean;
import bean.PostBean;
import bean.UserBean;

public class DBFacade {

	private static DBFacade instance;
	private static DAO<LastIdsBean> lastIds = LastIdsDAO.getInstance();
	private static DAO<UserBean> users = UserDAO.getInstance();
	private static DAO<PostBean> posts = PostDAO.getInstance();
	private static DAO<MediaBean> medias = MediaDAO.getInstance();

	private DBFacade() {
		
	}

	/**
	 * Singleton
	 * @return
	 */
	public static synchronized DBFacade getInstance(){
		if(instance == null)
			instance = new DBFacade();
		if (lastIds.list().size() == 0) { // This should not be here, but i have no idea where i need to put it.
			LastIdsBean ids = new LastIdsBean();
			lastIds.save(ids);
			}
		return instance;
	}

	/**
	 * This method will DELETE the entire DB
	 * Use it at your own risk.
	 */
	public void cleanPersistence(){
		File db = new File("./CommentDB.xml");
		db.delete();
		db = new File("./AnnouncementDB.xml");
		db.delete();
		db = new File("./BlogDB.xml");
		db.delete();
		db = new File("./MediaDB.xml");
		db.delete();
		db = new File("./PostDB.xml");
		db.delete();
		db = new File("./UserDB.xml");
		db.delete();
		db = new File("./LastIds.xml");
		db.delete();
	}

	public long getLastAnnouncementId() {
		Iterator<LastIdsBean> it = lastIds.list().iterator();

		if(it.hasNext()){
			LastIdsBean idBean = it.next();
			long id = idBean.getLastAnnouncementId();
			idBean.setLastAnnouncementId(id + 1);
			lastIds.update(idBean);
			return id;
		} else {
			return -1;
		}
	}

	public long getLastCommentId() {
		Iterator<LastIdsBean> it = lastIds.list().iterator();

		if(it.hasNext()){
			LastIdsBean idBean = it.next();
			long id = idBean.getLastCommentId();
			idBean.setLastCommentId(id + 1);
			lastIds.update(idBean);
			return id;
		} else {
			return -1;
		}
	}

	public long getLastMediaId() {
		Iterator<LastIdsBean> it = lastIds.list().iterator();
		
		if(it.hasNext()){
			LastIdsBean idBean = it.next();
			long id = idBean.getLastMediaId();
			idBean.setLastMediaId(id + 1);
			lastIds.update(idBean);
			return id;
		} else {
			return -1;
		}
	}

	public long getLastPostId() {
		Iterator<LastIdsBean> it = lastIds.list().iterator();
		
		if(it.hasNext()){
			LastIdsBean idBean = it.next();
			long id = idBean.getLastPostId();
			idBean.setLastPostId(id + 1);
			lastIds.update(idBean);
			return id;
		} else {
			return -1;
		}
	}

	public long getLastUserId() {
		Iterator<LastIdsBean> it = lastIds.list().iterator();
		if(it.hasNext()){
			LastIdsBean idBean = it.next();
			long id = idBean.getLastUserId();
			idBean.setLastUserId(id + 1);
			lastIds.update(idBean);
			return id;
		} else {
			return -1;
		}
	}
	
	public long getLastBlogId() {
		Iterator<LastIdsBean> it = lastIds.list().iterator();

		if(it.hasNext()){
			LastIdsBean idBean = it.next();
			long id = idBean.getLastBlogId();
			idBean.setLastBlogId(id + 1);
			lastIds.update(idBean);
			return id;
		} else {
			return -1;
		}
	}

	public void saveUsers(Collection<UserBean> values) {
		// TODO Auto-generated method stub
		
	}

	public List<UserBean> getUsers() {
		return users.list();
	}

	public List<BlogBean> getBlogsByUser(String userId) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<PostBean> getPostsByBlog(String blogId) {
		List<PostBean> blogPosts = new ArrayList<PostBean>();
		Iterator<PostBean> postsIT = posts.list().iterator();
		while(postsIT.hasNext()){
			PostBean bean = postsIT.next();
			if(bean.getOwner().equals(blogId)){
				blogPosts.add(bean);
			}
		}
		return blogPosts;
	}

	public List<CommentBean> getCommentsByPost(String postId) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<MediaBean> getMediasByPost(String postID) {
		List<MediaBean> postMedia = new ArrayList<MediaBean>();
		Iterator<MediaBean> postsIT = medias.list().iterator();
		while(postsIT.hasNext()){
			MediaBean bean = postsIT.next();
			if(bean.getOwner().equals(postID)){
				postMedia.add(bean);
			}
		}
		return postMedia;
	}

	public void saveComments(List<CommentBean> comments) {
		// TODO Auto-generated method stub
		
	}

	public void saveMedias(List<MediaBean> media) {
		// TODO Auto-generated method stub
		
	}

	public void savePosts(List<PostBean> values) {
		// TODO Auto-generated method stub
		
	}

	public void saveBlogs(List<BlogBean> values) {
		// TODO Auto-generated method stub
		
	}


}
